package com.atguigu.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.atguigu.eduservice.entity.EduSubject;
import com.atguigu.eduservice.entity.excel.SubjectData;
import com.atguigu.eduservice.entity.subject.OneSuject;
import com.atguigu.eduservice.entity.subject.TwoSubject;
import com.atguigu.eduservice.listener.SubjectExcelListener;
import com.atguigu.eduservice.mapper.EduSubjectMapper;
import com.atguigu.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author young
 * @since 2021-01-28
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    //添加课程分类
    @Override
    public void saveSubject(MultipartFile file,EduSubjectService subjectService) {
        try{
            //得到文件输入流
            InputStream in = file.getInputStream();
            //调用方法进行读取
            EasyExcel.read(in, SubjectData.class,new SubjectExcelListener(subjectService)).sheet().doRead();

        }catch (Exception e){
               e.printStackTrace();
        }

    }


    //课程分类列表树形
    @Override
    public List<OneSuject> getAllOneTwoSubject() {

        //1.查询所有的一级分类  parent_id = 0
        //根据条件查询多个
        QueryWrapper<EduSubject> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("parent_id","0");
        //这里查询的是多个，不能用getOne了

        //第一种写法用baseMapper里面的selectList
        List<EduSubject> oneSubjectList = baseMapper.selectList(wrapperOne);

        //第二种写法//this.list(wrapperOne)

        //2。查询所有的二级分类   parent_id != 0
        //根据条件查询多个
        QueryWrapper<EduSubject> wrapperTwo = new QueryWrapper<>();
        wrapperOne.ne("parent_id","0");
        List<EduSubject> twoSubjectList = baseMapper.selectList(wrapperTwo);

        //首先创建一个list集合用于存储最终封装的数据
        List<OneSuject> finalSubjectList = new ArrayList<>();

        //3、封装一级分类
        //查询出来所有的一级分类，然后遍历list集合，得到每一个一级分类的对象，获取每一个一级分类对象值
        //封装到要求的list集合里面List<OneSuject> finalSubjectList
        for (int i = 0; i < oneSubjectList.size(); i++) {  //遍历这个集合
            //得到oneSubjectList每一个eduSubject对象
            EduSubject eduSubject = oneSubjectList.get(i);

            //把eduSubject里面值获取出来，放到OneSubject对象里面
            //多个OneSubject放到finalSubjectList里面

            OneSuject oneSuject = new OneSuject();
//            oneSuject.setId(eduSubject.getId());
//            oneSuject.setTitle(eduSubject.getTitle());
            //简化写法,把eduSubject值复制到对应oneSuject对象里面去，他会找到对应的值（相同属性）进行赋值
            BeanUtils.copyProperties(eduSubject,oneSuject);

            //finalSubjectList.add(oneSuject);//有一个疑问这个不是应该放在最后面吗？？？？？？

            //在一级分类循环里遍历查询所有的一级分类，进行匹配赋值
            //先创建list集合封装每一个一级分类的二级分类
            List<TwoSubject> twoFinalSubjectList = new ArrayList<>();
            //遍历二级分类list集合
            for (int m = 0; m < twoSubjectList.size(); m++) {
                //获取每一个二级分类
                EduSubject tSubject = twoSubjectList.get(m);
                //判断二级分类的parentId和一级分类的id是否一样
                //eduSubject,oneSuject用那个都可以
                if (tSubject.getParentId().equals(eduSubject.getId())){
                    //把tSubject值复制到twoSubject里面，在放到twoFinalSubjectList里面
                    TwoSubject twoSubject = new TwoSubject();
                    BeanUtils.copyProperties(tSubject,twoSubject);
                    twoFinalSubjectList.add(twoSubject);
                }
            }
            //把一级下面的所有二级分类放到一级分类里面去
            oneSuject.setChildren(twoFinalSubjectList);
            finalSubjectList.add(oneSuject);

        }
        return finalSubjectList;
    }
}
